from predict_label import predict
def load_conf_labels():
    all_labels = []
    with open("label.conf","r") as fr:
        for line in fr:
            values=line.strip().split("_")
            #print(values)
            if len(values) !=3 or str(values[2])!= "1":
                print(values)
                continue
            #print(values)
            all_labels.append({"labelName":values[0],"labelId":values[1]})
    return all_labels
def get_conf_labels_id():
    label_id={}
    label_name={}
    label_dic={}
    with open("label.conf","r") as fr:
        for line in fr:
            values=line.strip().split("_")
            if len(values) !=3 or str(values[2])!= "1":
                continue
            label_id[values[0]]=values[1]
    with open("label_english_name.conf","r") as fr:
        for line in fr:
            values=line.strip().split("@")
            #print(values)
            if len(values) !=2:
                #print(values)
                continue
            #print(values)
            #all_labels.append({"labelName":values[0],"labelId":values[1]})
            label_name[values[1]]=values[0]
    return label_id,label_name

class conduct_script():
    def __init__(self,data,label_list,top_count,maxvalue):
        self.data = data
        self.predict = predict(data,maxvalue)
        self.label_list = label_list
        self.label_level = {}
        self.maxvalue = maxvalue
        self.top_count = top_count
        self.productDesc = data["productDesc"]
    def match_label(self):
        label_dict = self.predict.predict_label()
        for key in self.label_list:
            if self.label_list[key] >= 1 and label_dict[key] >= 1:
                label_dict[key] = self.label_list[key]
            else:
                label_dict[key] = 0
        return label_dict
    
    def sort_level_script(self,label_script,label_level):
        #print(label_level)
        label_dic_list = sorted(label_level.items(),key=lambda x: x[1],reverse=True)

        #key = label_dic_list[0][0]
        #str_content = label_script[key]
        str_content = []
        for var in label_dic_list:
            key = var[0]
            if key in label_script:
                str_content.append(label_script[key])
        #print()
        return str_content
    def out_content(self):
        label_script,label_level,script_label_dic,label_words,name_label_dic = self.predict.compute_level_script()
        res_script = self.sort_level_script(label_script,label_level)
        res_label_word = self.sort_level_script(label_words,name_label_dic)
        score = 0
        for key in script_label_dic:
            score+=script_label_dic[key]*5

        '''print(res_script)
        print(res_label_word)
        print(score)'''
        res_dict = {}
        res_dict["user_label"] = []
        res_dict["first_script"] = ""
        res_dict["scene_script"] = res_script
        res_dict["scene_label"] = res_label_word
     
        return res_dict
    
if __name__=='__main__':
        
        data = { 
        "is_gprs_over" : 1,"is_gsm_over" : 0,"gprs_all" : 0,"gsm_all" : 0,"now_reduce_price":0,
        "gprs_residua" : 10,"gsm_residua" : 10,"avg_day_gprs" : 20,"avg_day_gsm" : 20,
        "suf_current_day_count" : 20,"avg_three_month_gsm_count" : 2, "avg_three_month_gprs_count" : 3,
        "avg_three_month_gprs_fee": 0,"avg_three_month_gsm_fee":0,"tcw_gprs_fee1" :0,
        "tcw_gsm_fee1" : 10,"gprs_all1" : 0,"gprs_all2" : 0,"gprs_all3" : 0,"gprs_main_all1" : 0,"gprs_main_all2" : 0,"gprs_main_all3" : 0,"tcw_gprs1" : 0,
        "tcw_gprs_fee3":0,
        "tcw_gsm_fee3":10,
        "tcw_gprs3":0,
        "tcw_gsm3":0,
        "gsm_zj1" : 0,"tcw_gsm1": 0,"gsm_main_all1" : 0,"gsm_main_all2" : 0,"gsm_main_all3" : 0,"tcw_gprs_fee2" : 0,"tcw_gsm_fee2" : 0,
        "tcw_gprs2" : 0,"tcw_gsm2" : 0,"is_over_activity" : 0, "pre_day_num" : 1,"current_month_num" : 1,
        "next_day_num" :1,"next_month_num" : 1,"now_prod_value" :19200,"now_prod_gprs" : 100,"now_prod_gsm" : 100,
        "gprs_all" : 100,"gsm_all" :100,"avg_three_month_gsm" : 0,"avg_three_month_gprs":0,
        "is_limit":2,
        "bill_fee1":0,
        "bill_fee2":0,
        "bill_fee3":0,
        "avg_three_month_shorttime_packet_ordertime":["yyyyMMddHHmmss","yyyyMMddHHmmss"],
        "productDesc":"现为你推荐一款更优惠的套餐，我为你介绍一下，套餐原价69元/月，折扣后仅需59元，包含20G流量，400分钟通话"
    }
        print("start")
        top_count = 3,
        maxvalue = {}
        label_list = []
        p = conduct_script(data,label_list,top_count,maxvalue)
        values= p.out_content()
        print("匹配的用户标签：")
        #print(str(values).decode('string_escape'))
        

